package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"os"

	"github.com/xinminsu/go-mod-core-contracts/models"
)

func main() {
	go  runTest0()
	//go  runTest1()
	select {}

}

func runTest1(){
    data,err := ioutil.ReadFile("../j1.txt")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Fprintf(os.Stdout, "data = %s .\n",string(data))
    dpSlice := make([]models.DeviceProfile, 4)
    err = json.Unmarshal(data, &dpSlice)

	fmt.Fprintf(os.Stdout, "dpSlice.length = %d .\n", len(dpSlice))

	fmt.Fprintf(os.Stdout, "dpSlice[0].DescribedObject = %v .\n", dpSlice[0].DescribedObject)
	fmt.Fprintf(os.Stdout, "dpSlice[0].Id = %s .\n", dpSlice[0].Id)
	fmt.Fprintf(os.Stdout, "dpSlice[0].Name = %s .\n", dpSlice[0].Name)
	fmt.Fprintf(os.Stdout, "dpSlice[0].Manufacturer = %s .\n", dpSlice[0].Manufacturer)
	fmt.Fprintf(os.Stdout, "dpSlice[0].Model = %s .\n", dpSlice[0].Model)
	fmt.Fprintf(os.Stdout, "dpSlice[0].Labels = %v .\n", dpSlice[0].Labels)
	fmt.Fprintf(os.Stdout, "dpSlice[0].DeviceResources = %v .\n", dpSlice[0].DeviceResources)
	fmt.Fprintf(os.Stdout, "dpSlice[0].DeviceCommands = %v .\n", dpSlice[0].DeviceCommands)
	fmt.Fprintf(os.Stdout, "dpSlice[0].CoreCommands = %v .\n", dpSlice[0].CoreCommands)

	fmt.Fprintf(os.Stdout, "dpSlice[1].DescribedObject = %v .\n", dpSlice[1].DescribedObject)
	fmt.Fprintf(os.Stdout, "dpSlice[1].Id = %s .\n", dpSlice[1].Id)
	fmt.Fprintf(os.Stdout, "dpSlice[1].Name = %s .\n", dpSlice[1].Name)
	fmt.Fprintf(os.Stdout, "dpSlice[1].Manufacturer = %s .\n", dpSlice[1].Manufacturer)
	fmt.Fprintf(os.Stdout, "dpSlice[1].Model = %s .\n", dpSlice[1].Model)
	fmt.Fprintf(os.Stdout, "dpSlice[1].Labels = %v .\n", dpSlice[1].Labels)
	fmt.Fprintf(os.Stdout, "dpSlice[1].DeviceResources = %v .\n", dpSlice[1].DeviceResources)
	fmt.Fprintf(os.Stdout, "dpSlice[1].DeviceCommands = %v .\n", dpSlice[1].DeviceCommands)
	fmt.Fprintf(os.Stdout, "dpSlice[1].CoreCommands = %v .\n", dpSlice[1].CoreCommands)

	fmt.Fprintf(os.Stdout, "dpSlice[2].DescribedObject = %v .\n", dpSlice[2].DescribedObject)
	fmt.Fprintf(os.Stdout, "dpSlice[2].Id = %s .\n", dpSlice[2].Id)
	fmt.Fprintf(os.Stdout, "dpSlice[2].Name = %s .\n", dpSlice[2].Name)
	fmt.Fprintf(os.Stdout, "dpSlice[2].Manufacturer = %s .\n", dpSlice[2].Manufacturer)
	fmt.Fprintf(os.Stdout, "dpSlice[2].Model = %s .\n", dpSlice[2].Model)
	fmt.Fprintf(os.Stdout, "dpSlice[2].Labels = %v .\n", dpSlice[2].Labels)
	fmt.Fprintf(os.Stdout, "dpSlice[2].DeviceResources = %v .\n", dpSlice[2].DeviceResources)
	fmt.Fprintf(os.Stdout, "dpSlice[2].DeviceCommands = %v .\n", dpSlice[2].DeviceCommands)
	fmt.Fprintf(os.Stdout, "dpSlice[2].CoreCommands = %v .\n", dpSlice[2].CoreCommands)

	fmt.Fprintf(os.Stdout, "dpSlice[3].DescribedObject = %v .\n", dpSlice[3].DescribedObject)
	fmt.Fprintf(os.Stdout, "dpSlice[3].Id = %s .\n", dpSlice[3].Id)
	fmt.Fprintf(os.Stdout, "dpSlice[3].Name = %s .\n", dpSlice[3].Name)
	fmt.Fprintf(os.Stdout, "dpSlice[3].Manufacturer = %s .\n", dpSlice[3].Manufacturer)
	fmt.Fprintf(os.Stdout, "dpSlice[3].Model = %s .\n", dpSlice[3].Model)
	fmt.Fprintf(os.Stdout, "dpSlice[3].Labels = %v .\n", dpSlice[3].Labels)
	fmt.Fprintf(os.Stdout, "dpSlice[3].DeviceResources = %v .\n", dpSlice[3].DeviceResources)
	fmt.Fprintf(os.Stdout, "dpSlice[3].DeviceCommands = %v .\n", dpSlice[3].DeviceCommands)
	fmt.Fprintf(os.Stdout, "dpSlice[3].CoreCommands = %v .\n", dpSlice[3].CoreCommands)


}


func runTest0(){
	/*data,err := ioutil.ReadFile("../j0.txt")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Fprintf(os.Stdout, "data = %s .\n",string(data))
	var dpSlice models.DeviceProfile
	err = json.Unmarshal(data, &dpSlice)

	fmt.Println("dpSlice = ",dpSlice)*/


	str2 := `{"created": 1559790450892,"modified": 1559790450892,"origin": 1559790450832,"description": "GS1 AC Drive Motor Controller","id": "5cf883729008b0a96e6e0c1d","name": "GS1-VariableSpeedMotor","manufacturer": "Automation Direct","model": "GS1-10P5",
"labels": ["motor", "Industrial", "Motor Controller", "variable speed"],
"deviceResources": [{
		"description": "PWM Output Frequency for driving motor.",
		"name": "OutputFrequency",
		"properties": {
			"value": {
				"type": "Integer",
				"readWrite": "RW",
				"defaultValue": "1",
				"size": "2",
				"mask": "0x00",
				"shift": "0",
				"scale": "1.0",
				"offset": "0.0",
				"base": "0"
			},
			"units": {
				"type": "String",
				"readWrite": "R",
				"defaultValue": "Hz"
			}
		},
		"attributes": {
			"HoldingRegister": "8451"
		}
	}, {
		"description": "RPM speed of the motor.",
		"name": "RPM",
		"properties": {
			"value": {
				"type": "Float",
				"readWrite": "RW",
				"minimum": "0",
				"maximum": "1500",
				"defaultValue": "1",
				"size": "2",
				"mask": "0x00",
				"shift": "0",
				"scale": "1.0",
				"offset": "0.0",
				"base": "0"
			},
			"units": {
				"type": "String",
				"readWrite": "R",
				"defaultValue": "rpm"
			}
		},
		"attributes": {
			"HoldingRegister": "8455"
		}
	}, {
		"description": "Output Voltage driving the motor.",
		"name": "OutputVoltage",
		"properties": {
			"value": {
				"type": "Float",
				"readWrite": "RW",
				"defaultValue": "1",
				"size": "2",
				"mask": "0x00",
				"shift": "0",
				"scale": "0.1",
				"offset": "0.0",
				"base": "0"
			},
			"units": {
				"type": "String",
				"readWrite": "R",
				"defaultValue": "Volts"
			}
		},
		"attributes": {
			"HoldingRegister": "8454"
		}
	}, {
		"description": "On/Off Status of the motor.",
		"name": "Status",
		"properties": {
			"value": {
				"type": "Integer",
				"readWrite": "RW",
				"minimum": "0",
				"maximum": "1",
				"defaultValue": "0",
				"size": "2",
				"mask": "0x00",
				"shift": "0",
				"scale": "1.0",
				"offset": "0.0",
				"base": "0"
			},
			"units": {
				"type": "String",
				"readWrite": "R",
				"defaultValue": "Enabled/Disabled"
			}
		},
		"attributes": {
			"HoldingRegister": "2331"
		}
	}, {
		"description": "Clockwise/Counterclockwise Direction of the motor.",
		"name": "Direction",
		"properties": {
			"value": {
				"type": "Integer",
				"readWrite": "RW",
				"minimum": "0",
				"maximum": "1",
				"defaultValue": "0",
				"size": "2",
				"mask": "0x00",
				"shift": "0",
				"scale": "1.0",
				"offset": "0.0",
				"base": "0"
			},
			"units": {
				"type": "String",
				"readWrite": "R",
				"defaultValue": "Clockwise/CounterClockwise"
			}
		},
		"attributes": {
			"HoldingRegister": "2332"
		}
	}, {
		"description": "PWM Frequency driving the motor.",
		"name": "Frequency",
		"properties": {
			"value": {
				"type": "Integer",
				"readWrite": "RW",
				"defaultValue": "0",
				"size": "2",
				"mask": "0x00",
				"shift": "0",
				"scale": "2.875",
				"offset": "0.0",
				"base": "0"
			},
			"units": {
				"type": "String",
				"readWrite": "R",
				"defaultValue": "Hz"
			}
		},
		"attributes": {
			"HoldingRegister": "2330"
		}
	}],
"deviceCommands": [{
		"name": "OutputVoltage",
		"get": [{
			"operation": "get",
			"object": "OutputVoltage",
			"parameter": "OutputVoltage"
		}]
	}, {
		"name": "Status",
		"get": [{
			"operation": "get",
			"object": "Status",
			"parameter": "Status"
		}],
		"set": [{
			"index": "1",
			"operation": "set",
			"object": "Status",
			"parameter": "Status"
		}]
	}, {
		"name": "RPM",
		"get": [{
			"operation": "get",
			"object": "RPM",
			"parameter": "RPM"
		}],
		"set": [{
			"index": "1",
			"operation": "set",
			"object": "Status",
			"parameter": "1"
		}, {
			"index": "2",
			"operation": "set",
			"object": "Frequency",
			"parameter": "RPM"
		}]
	}, {
		"name": "AllValues",
		"get": [{
			"index": "1",
			"operation": "get",
			"object": "RPM",
			"parameter": "RPM"
		}, {
			"index": "2",
			"operation": "get",
			"object": "OutputVoltage",
			"parameter": "OutputVoltage"
		}]
	}, {
		"name": "MotorControl",
		"get": [{
			"operation": "get",
			"resource": "AllValues"
		}],
		"set": [{
			"operation": "set",
			"parameter": "RPM",
			"resource": "RPM"
		}]
	}],
"coreCommands": [{
		"get": {},
		"put": {}
	}]}`

	var sd models.DeviceProfile

	json.Unmarshal([]byte(str2), &sd)
	fmt.Println("sd = ",sd)



}